package com.yuncheng.oaApi.controller;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.spcyApi.utils.MyExcelUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.OaJbzbgl;
import com.yuncheng.oaApi.service.IOaJbzbglService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description: oa_jbzbgl
 * @Author: yuncheng
 * @Date: 2023-09-08
 * @Version: V1.0
 */
@Api(tags = "加班值班接口")
@RestController
@RequestMapping("/api/spzx-oa/oaJbzbgl")
public class OaJbzbglController extends BaseController<OaJbzbgl, IOaJbzbglService> {

    private static final Logger log = LoggerFactory.getLogger(OaJbzbglController.class);

    @Resource
    private IOaJbzbglService oaJbzbglService;

    @Resource
    private MyExcelUtils oaMyExcelUtils;

    /**
     * 分页列表查询
     *
     * @param oaJbzbgl
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "oa_jbzbgl-分页列表查询")
    @ApiOperation(value = "oa_jbzbgl-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaJbzbgl oaJbzbgl, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<OaJbzbgl> queryWrapper = QueryBuilder.initQueryWrapper(oaJbzbgl, req.getParameterMap());
        Page<OaJbzbgl> page = new Page<>(pageNo, pageSize);
        IPage<OaJbzbgl> pageList = oaJbzbglService.page(page, queryWrapper);
        for (OaJbzbgl vo : pageList.getRecords()) {
            formatValue(vo);
        }
        return HttpResult.ok(pageList);
    }


    /**
     * 编辑
     *
     * @param oaJbzbgl
     * @return
     */
    @AutoLog(value = "oa_jbzbgl-编辑")
    @ApiOperation(value = "oa_jbzbgl-编辑")
    //@RequiresPermissions("jbzb:OaJbzbgl:edit")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody OaJbzbgl oaJbzbgl) {
        oaJbzbglService.updateById(oaJbzbgl);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    @AutoLog(value = "导出加班值班数据-表格excel")
    @ApiOperation(value = "导出加班值班数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "主键id字符逗号分割",required = true)
    })
    @GetMapping("/exportqxjData")
    public void exportqxjData(@RequestParam(value = "ids",defaultValue = "")String ids,
                              OaJbzbgl ndkpBasic,
                              HttpServletResponse response){
        try {
            String name = "exportqxjData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出
            if (StringUtils.isNotEmpty(ids)) {
                List<OaJbzbgl> ndkpBasicList = oaJbzbglService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaJbzbgl e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }else {
                LambdaQueryWrapper<OaJbzbgl> queryWrapper = new LambdaQueryWrapper<>();
                //部门
                queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFbm()),OaJbzbgl::getFbm, ndkpBasic.getFbm());
                //姓名
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFxm()),OaJbzbgl::getFxm,ndkpBasic.getFxm());
                //加班时长
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFjbzbsc()),OaJbzbgl::getFjbzbsc,ndkpBasic.getFjbzbsc());

                //加班开始时间
                if (ndkpBasic.getFjbzbkssj() != null){
                    queryWrapper.ge(OaJbzbgl::getFjbzbkssj,ndkpBasic.getFjbzbkssj());
                }
                //加班结束时间
                if (ndkpBasic.getFjbzbjssj() != null){
                    queryWrapper.le(OaJbzbgl::getFjbzbjssj,ndkpBasic.getFjbzbjssj());
                }





                List<OaJbzbgl> ndkpBasicList = oaJbzbglService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaJbzbgl e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }
            oaMyExcelUtils.outputListToExclFile(OaBusinessConstant.IMPORT_OA_QXJSP_XML,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出行政事务审批数据出错-{}",e.getMessage());
        }
    }
    /**
     * 数据转换
     *
     * @param oaJbzbgl
     */
    private void formatValue(OaJbzbgl oaJbzbgl) {
    }
}
